import 'dart:io';
import 'package:android_intent_plus/android_intent.dart';
import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/config/app_bar_view.dart';
import 'package:permission_handler/permission_handler.dart';


class PermissionManagePage extends StatefulWidget {
 final String title;
  const PermissionManagePage({this.title='',  super.key});

  @override
  State<PermissionManagePage> createState() => _PermissionManagePageState();
}

class _PermissionManagePageState extends State<PermissionManagePage> with WidgetsBindingObserver {
  bool _approved = false;
  bool _cameraApproved = false;

  @override
  void initState() {
    loadStatus();
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  loadStatus() async {
    var status = await Permission.storage.status;
    var cameraStatus= await Permission.camera.status;
    setState(() {
      _approved = !status.isDenied &&  !status.isPermanentlyDenied;
      _cameraApproved=cameraStatus.isGranted;
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      loadStatus();
    }
  }

  void onTapItem() async {
    if(Platform.isAndroid){
      AndroidIntent intent = const AndroidIntent(
        action: "android.settings.APPLICATION_DETAILS_SETTINGS",
        package: "com.aispheretech.xinghehuayu",
        data: "package:com.aispheretech.xinghehuayu",
      );
      intent.launch();
      await intent.launch();
    }else{
      AppSettings.openAppSettings(type: AppSettingsType.settings);

    }

  }

  @override
  Widget build(BuildContext context) {
    return CommonAppWidget.navAppBar(
      title:widget.title ,
      childWidget: ListView(
        children: [
          Container(
            margin: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  spreadRadius: 1,
                  blurRadius: 1,
                  offset: const Offset(0, 1), // changes position of shadow
                ),
              ],
            ),
            child: ListTile(
              onTap: onTapItem,
              title: const Text('访问相册'),
              subtitle: const Text('允许星河画语读取您相册存储中的图片，用于导入图片帮助您进行后续编辑处理、意见反馈'),
              trailing: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    _approved ? '已开启' : '去设置',
                    style: const TextStyle(fontSize: 14),
                  ),
                  const Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey,
                    size: 14,
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  spreadRadius: 1,
                  blurRadius: 1,
                  offset: const Offset(0, 1), // changes position of shadow
                ),
              ],
            ),
            child: ListTile(
              onTap: onTapItem,
              title: const Text('访问相机'),
              subtitle: const Text('允许星河画语使用你的相机,用于拍照导入图片编辑'),
              trailing: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    _cameraApproved ? '已开启' : '去设置',
                    style: const TextStyle(fontSize: 14),
                  ),
                  const Icon(
                    Icons.arrow_forward_ios,
                    color: Colors.grey,
                    size: 14,
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
